package com.atguigu.eduservice.service.impl;

import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.cron.vo.ResCollectVo;
import com.atguigu.eduservice.entity.EduCourseCollect;
import com.atguigu.eduservice.mapper.EduCourseCollectMapper;
import com.atguigu.eduservice.service.EduCourseCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-03-16
 */
@Service
public class EduCourseCollectServiceImpl extends ServiceImpl<EduCourseCollectMapper, EduCourseCollect> implements EduCourseCollectService {

    /**
     * 取消收藏
     *
     * @param courseId
     * @param memberId
     */
    @Override
    public void cancelCollect(String courseId, String memberId) {
        baseMapper.deleteById(selectCollectId(courseId, memberId));
    }

    @Override
    public PageListBean queryPage(Integer pageNo, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNo, pageSize);
        List<ResCollectVo> collectVos = baseMapper.selectCollects(memberId);
        PageInfo pageInfo = new PageInfo(collectVos);
        return new PageListBean((int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean isCollect(String courseId, String memberId) {
        String collectId = selectCollectId(courseId, memberId);
        return !StringUtils.isEmpty(collectId);
    }

    @Override
    public void hitCollect(EduCourseCollect collect) {
        baseMapper.insert(collect);
    }

    /**
     * 查询出用户收藏的所有课程的id
     *
     * @param memberId
     * @return
     */
    private List<String> selectCourseIds(String memberId) {
        List<String> resList = new ArrayList<>();
        QueryWrapper<EduCourseCollect> qw = new QueryWrapper<>();
        qw.select("course_id");
        qw.eq("member_id", memberId);
        List<EduCourseCollect> collects = baseMapper.selectList(qw);
        collects.forEach((item) -> {
            resList.add(item.getCourseId());
        });
        return resList;
    }

    /**
     * 根据课程id和用户id查询出收藏id
     *
     * @param courseId
     * @param memberId
     */
    private String selectCollectId(String courseId, String memberId) {
        QueryWrapper<EduCourseCollect> qw = new QueryWrapper<>();
        qw.eq("course_id", courseId);
        qw.eq("member_id", memberId);
        qw.last("limit 1");
        EduCourseCollect collect = baseMapper.selectOne(qw);
        if (!StringUtils.isEmpty(collect)) {
            return collect.getId();
        } else {
            return "";
        }

    }
}
